'use strict';
app.service('noticeService',['$http','$q','utilService',function($http,$q,utilService){
//新增公告
    this.addNotice = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice',
            data:$scope.notice,
            method:'POST'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
//根据id查询公告
    this.findById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'notice/'+ $scope.notice.id,
            params:{id:$scope.notice.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }
//修改公告
    this.editNotice = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice',
            data:$scope.notice,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
//禁用启用
    this.changeStatus = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice/change/status',
            data:{id:$scope.notice.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
    //使失效
    this.makeFailure = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice/failure',
            data:{id:$scope.notice.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
//置顶
    this.toTop = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice/toTop',
            data:{id:$scope.notice.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
//删除公告
    this.delNotice = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'notice/del',
            data:{id:$scope.notice.id},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

}]);